﻿

#include "RootWindow.h"
#include "WeatherViewController.h"
#include "SwitchCityController.h"


RootWindow* RootWindow::getInstance()
{
	static RootWindow* _window = NULL;

	if (_window == NULL)
	{
		_window = new RootWindow();
		_window->init();
		_window->autorelease();
	}
	return _window;
}

RootWindow::RootWindow()
{
	std::string fullPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "weather_default_city.db";
	localStorageInit(fullPath.c_str());
	loadCityConfigs();
}

RootWindow::~RootWindow()
{
	localStorageFree();
}

bool RootWindow::init()
{
    if (!CAWindow::init())
    {
        return false;
    }
    
	WeatherViewController* _viewController = new WeatherViewController();
    _viewController->init();
	_viewController->autorelease();
    
	m_pRootNavigationController = new CANavigationController();
	m_pRootNavigationController->setNavigationBarBackgroundImage(CAImage::create("r/navbg.jpg"));
	m_pRootNavigationController->setNavigationBarHidden(true, false);
	m_pRootNavigationController->initWithRootViewController(_viewController);
	this->setRootViewController(m_pRootNavigationController);

    return true;
}

void RootWindow::loadCityConfigs()
{
	std::string szData = CCFileUtils::sharedFileUtils()->getFileString("config/city.cfg");

	std::vector<std::string> v = Parse2StrVector(szData, ";");
	for (int i = 0; i < v.size(); i++)
	{
		std::vector<std::string> vv = Parse2StrVector(v[i], ",");
		if (vv.empty()) continue;
		m_vAllCitys.push_back(vv);
	}
}

const std::vector< std::vector<std::string> >& RootWindow::getAllCitys()
{
	return m_vAllCitys;
}

std::string RootWindow::getCurCityName()
{
	int i, j;
	if (getCurProvinceAndCity(i, j))
	{
		const std::vector<std::string>& v = m_vAllCitys[i];
		if (v.size() > 1)
		{
			return v[j + 1];
		}
		else
		{
			return v[j];
		}
	}
	return "";
}

bool RootWindow::getCurProvinceAndCity(int& i, int& j)
{
	const char* pStr = localStorageGetItem("province_city");
	if (pStr == NULL)
	{
		return false;
	}

	std::vector<std::string> v = Parse2StrVector(pStr, "|");
	if (v.size() != 2)
	{
		return false;
	}
	i = atoi(v[0].c_str());
	j = atoi(v[1].c_str());
	return true;
}

void RootWindow::setCurProvinceAndCity(int i, int j)
{
	localStorageSetItem("province_city", crossapp_format_string("%d|%d", i, j).c_str());
}

void RootWindow::keyBackClicked()
{
	CC_RETURN_IF(CAAlertView::hideWithDisplayed());

	if (this->getModalViewController())
	{
		this->dismissModalViewController(true);
	}
	else if (this->getRootNavigationController()->getViewControllerCount() > 1)
	{
		this->getRootNavigationController()->popViewControllerAnimated(true);
	}
	else
	{
		CAApplication::getApplication()->end();
	}
}

void RootWindow::BackToPreviousController()
{
	getRootNavigationController()->popViewControllerAnimated(true);
}

void RootWindow::EnterToSwitchCityController()
{
	CSwitchCityController* cityController = new CSwitchCityController;
	cityController->init();
	cityController->setNavigationBarItem(CANavigationBarItem::create(UTF8("切换城市")));
	cityController->autorelease();
	getRootNavigationController()->pushViewController(cityController, true);
}
